package cn.zhz.gen.core;

import java.util.Arrays;
import java.util.List;

/**
 * @author ZHZ
 * @since 2022-11-06
 */
public class FastGenerator {

    private final String url;

    private final String username;

    private final String password;

    /**
     * 全局配置
     */
    private FastGlobalConfig fastGlobalConfig = FastGlobalConfig.create();

    /**
     * controller配置
     */
    private FastControllerConfig fastControllerConfig = FastControllerConfig.create();

    /**
     * model配置(dto,po,vo)
     */
    private FastModelConfig fastModelConfig = FastModelConfig.create();

    /**
     * 表名列表
     */
    private List<String> tableNames;

    /**
     * 文件生成路径
     */
    private String outputDir;

    /**
     * 过滤表后缀
     * example: addTableSuffix("_0")
     * result: t_simple_0 -> Simple
     */
    private String[] tableSuffix = {};

    /**
     * 过滤表前缀
     * example: addTablePrefix("t_")
     * result: t_simple -> Simple
     */
    private String[] tablePrefix = {};


    private FastGenerator(String url, String username, String password) {
        this.url = url;
        this.username = username;
        this.password = password;
    }

    /**
     * 第一步创建（唯一入口）
     *
     * @param url
     * @param username
     * @param password
     * @return
     */
    public static FastGenerator create(String url, String username, String password) {
        return new FastGenerator(url, username, password);
    }

    /**
     * 全局配置
     *
     * @param fastGlobalConfig
     * @return
     */
    public FastGenerator globalConfig(FastGlobalConfig fastGlobalConfig) {
        this.fastGlobalConfig = fastGlobalConfig;
        return this;
    }

    /**
     * controller配置
     *
     * @param fastControllerConfig
     * @return
     */
    public FastGenerator controllerConfig(FastControllerConfig fastControllerConfig) {
        this.fastControllerConfig = fastControllerConfig;
        return this;
    }

    /**
     * model配置
     *
     * @param fastModelConfig
     * @return
     */
    public FastGenerator modelConfig(FastModelConfig fastModelConfig) {
        this.fastModelConfig = fastModelConfig;
        return this;
    }

    /**
     * 配置表名，多个英文逗号分隔（所有输入 all）
     *
     * @param tableNames
     * @return
     */
    public FastGenerator tableNames(String... tableNames) {
        this.tableNames = Arrays.asList(tableNames);
        return this;
    }

    /**
     * 配置文件生成路径
     *
     * @param outputDir
     * @return
     */
    public FastGenerator outputDir(String outputDir) {
        this.outputDir = outputDir;
        return this;
    }


    /**
     * 增加过滤表前缀
     *
     * @param tablePrefix 过滤表前缀
     * @return this
     */
    public FastGenerator tablePrefix(String... tablePrefix) {
        this.tablePrefix = tablePrefix;
        return this;
    }

    /**
     * 增加过滤表后缀
     *
     * @param tableSuffix 过滤表后缀
     * @return this
     */
    public FastGenerator tableSuffix(String... tableSuffix) {
        this.tableSuffix = tableSuffix;
        return this;
    }


    /**
     * 运行
     */
    public void run() {
        CustomGenerateStyle customGenerateStyle = new CustomGenerateStyle(this);
        customGenerateStyle.beforeGenerate();
        customGenerateStyle.generateByConsole();
    }

    protected String getUrl() {
        return url;
    }

    protected String getUsername() {
        return username;
    }

    protected String getPassword() {
        return password;
    }

    protected List<String> getTableNames() {
        return tableNames;
    }

    protected String getOutputDir() {
        return outputDir;
    }

    protected String[] getTableSuffix() {
        return tableSuffix;
    }

    protected String[] getTablePrefix() {
        return tablePrefix;
    }

    protected FastGlobalConfig getFastGlobalConfig() {
        return fastGlobalConfig;
    }

    protected FastControllerConfig getFastControllerConfig() {
        return fastControllerConfig;
    }

    protected FastModelConfig getFastModelConfig() {
        return fastModelConfig;
    }
}
